package com.rth.common.context;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class LoginContextHolder {

   private final static InheritableThreadLocal<Map<String, Object>> threadLocal = new InheritableThreadLocal<Map<String, Object>>();



   public static  void remove(){
       threadLocal.remove();
   }

   public static Object get(String key) {
       Map<String, Object> objectMap = getThreadLocalMap();
       return objectMap.get(key);
   }

    public static void set(String key,Object value) {

        Map<String, Object> objectMap = getThreadLocalMap();
        objectMap.put(key,value);
    }


    public static String getLoginId(){

       return (String) getThreadLocalMap().get("loginId");

    }

    private static Map<String,Object> getThreadLocalMap() {

        Map<String, Object> objectMap = threadLocal.get();
        if (Objects.isNull(objectMap)) {
            objectMap = new ConcurrentHashMap<>();
            threadLocal.set(objectMap);
        }
        return objectMap;
    }

}
